<?php
/**
 * 创建平台权限角色关联
 * 2018/08/01
 * 郭森林
 * @return void
 */
use Illuminate\Database\Seeder;
use App\Models\AdminRole as Role;
use App\Models\AdminPermission as Permission;
use App\Models\AdminRolePermission as RolePermission;

class AdminRolePermissionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');//禁用外键约束
        DB::table('admin_role_permission')->truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');//启用外键约束
        // 查询平台内置角色
        $role_id = Role::where('name', '超级管理员')->first()->id;

        // 查询未关联的权限
        $data = Permission::select('id')->orderBy('sort','asc')->get()->toArray();

        // 循环数据, 预添加数据
        $inserts = array();
        foreach ($data as $v) {
            $inserts[] = [
                'role_id' => $role_id,
                'permission_id'=>$v['id'],
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ];
        }
        if (count($inserts)) {
            // 批量添加关联
            RolePermission::insert($inserts);
        }
    }
}
